// 通用状态字典
export const STATUS_DICT = {
    0: '未完成',
    1: '部分完成',
    2: '已完成',
}

// 任务状态常量
export const TASK_STATUS = {
    0: '未完成',
    1: '部分完成',
    2: '已完成',
}

// 任务状态对应的样式类
export const TASK_STATUS_CLASS = {
    0: 'pending',
    1: 'partial',
    2: 'completed',
}

// 农事类型常量
export const AGRICULTURAL_WORK_TYPES = {
    sowing_management: '播种',
    disaster_weather: '灾害',
    pest_weed_disease: '病虫草害',
    harvest_records: '采收',
    labor_management: '用工',
    fertilization_management: '施肥',
    growth_records: '作物生长',
    irrigation_drainage: '灌溉',
    land_preparation: '土地整备',
    plant_protection: '植保',
}
// 农事图表常量
export const AGRICULTURAL_WORK_TYPES_CHART = {
    // 播种
    sowing_management: '/static/images/work/sowing-icon.png',
    // 灾害
    disaster_weather: '/static/images/work/disaster-icon.png',
    // 病虫草害
    pest_weed_disease: '/static/images/work/pest-icon.png',
    // 采收
    harvest_records: '/static/images/work/harvest-icon.png',
    // 用工
    labor_management: '/static/images/work/labor-icon.png',
    // 施肥
    fertilization_management: '/static/images/work/fertilizer-icon.png',
    // 作物生长
    growth_records: '/static/images/work/growth-icon.png',
    // 灌溉
    irrigation_drainage: '/static/images/work/irrigation-icon.png',
    // 土地整备
    land_preparation: '/static/images/work/land-icon.png',
    // 植保
    plant_protection: '/static/images/work/protection-icon.png',
}

// 工作事项图标映射（每个类型包含选中和未选中两种状态）
const WORK_TYPE_ICONS = {
    // 播种
    sowing_management: {
        active: '/static/images/work/sowing-active.png',
        default: '/static/images/work/sowing.png',
    },
    // 灾害
    disaster_weather: {
        active: '/static/images/work/other-active.png',
        default: '/static/images/work/other.png',
        // active: '/static/images/work/disaster-active.png',
        // default: '/static/images/work/disaster.png',
    },
    // 病虫草害
    pest_weed_disease: {
        active: '/static/images/work/other-active.png',
        default: '/static/images/work/other.png',
        // active: '/static/images/work/pest-active.png',
        // default: '/static/images/work/pest.png',
    },
    // 采收
    harvest_records: {
        active: '/static/images/work/other-active.png',
        default: '/static/images/work/other.png',
        // active: '/static/images/work/harvest-active.png',
        // default: '/static/images/work/harvest.png',
    },
    // 用工
    labor_management: {
        active: '/static/images/work/other-active.png',
        default: '/static/images/work/other.png',
        // active: '/static/images/work/labor-active.png',
        // default: '/static/images/work/labor.png',
    },
    // 施肥
    fertilization_management: {
        active: '/static/images/work/fertilizer-active.png',
        default: '/static/images/work/fertilizer.png',
    },
    // 作物生长
    growth_records: {
        active: '/static/images/work/other-active.png',
        default: '/static/images/work/other.png',
        // active: '/static/images/work/growth-active.png',
        // default: '/static/images/work/growth.png',
    },
    // 灌溉
    irrigation_drainage: {
        active: '/static/images/work/irrigation-active.png',
        default: '/static/images/work/irrigation.png',
    },
    // 土地整备
    land_preparation: {
        active: '/static/images/work/other-active.png',
        default: '/static/images/work/other.png',
        // active: '/static/images/work/land-active.png',
        // default: '/static/images/work/land.png',
    },
    // 植保
    plant_protection: {
        active: '/static/images/work/protection-active.png',
        default: '/static/images/work/protection.png',
    },
}

/**
 * 将工作事项字典转换为选择器使用的格式
 * @returns {Array} 返回格式化后的数组，每项包含 value, label, activeIcon, defaultIcon
 */
export function getWorkTypeOptions() {
    return Object.entries(AGRICULTURAL_WORK_TYPES).map(([value, label]) => {
        const icons = WORK_TYPE_ICONS[value] || {
            active: '/static/images/work/other-active.png',
            default: '/static/images/work/other.png',
        }
        return {
            value,
            label,
            activeIcon: icons.active,
            defaultIcon: icons.default,
        }
    })
}

/**
 * 根据工作事项的值获取对应的文本标签
 * @param {string} value 工作事项的值
 * @returns {string} 返回对应的文本标签，如果未找到则返回空字符串
 */
export function getWorkTypeLabel(value) {
    return AGRICULTURAL_WORK_TYPES[value] || ''
}

/**
 * 根据工作事项值数组获取对应的文本标签数组
 * @param {string|Array} values 工作事项值或值的数组
 * @returns {string} 返回文本标签，多个用逗号分隔
 */
export function getWorkTypeLabels(values) {
    if (!values) return ''
    const valueArray = typeof values === 'string' ? values.split(',') : values
    return valueArray
        .map((value) => AGRICULTURAL_WORK_TYPES[value] || '')
        .filter(Boolean)
        .join(',')
}

/**
 * 通用函数：根据字典和键值获取对应的文字
 * @param {Object} dict 字典对象
 * @param {string|number|Array} key 键值或键值数组
 * @param {string} defaultValue 默认值，当未找到对应文字时返回
 * @returns {string} 返回对应的文字或多个文字（逗号分隔）
 */
export function getDictText(dict, key, defaultValue = '') {
    // 参数校验
    if (!dict || typeof dict !== 'object') {
        return defaultValue
    }

    // 处理数组情况
    if (Array.isArray(key)) {
        return key
            .map((k) => dict[k] || defaultValue)
            .filter(Boolean)
            .join(',')
    }

    // 处理字符串类型的逗号分隔值
    if (typeof key === 'string' && key.includes(',')) {
        return key
            .split(',')
            .map((k) => dict[k.trim()] || defaultValue)
            .filter(Boolean)
            .join(',')
    }

    // 处理单个值
    return dict[key] || defaultValue
}
